package com.hzncc.scaffold.controller.Rear.OnePlusX;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Bo.SiteManagementBo;
import com.hzncc.scaffold.entity.Other.Siteapply;
import com.hzncc.scaffold.entity.Other.Tag;
import com.hzncc.scaffold.entity.Other.User;
import com.hzncc.scaffold.entity.Other.Usertag;
import com.hzncc.scaffold.entity.Vo.*;
import com.hzncc.scaffold.mapper.SiteapplyMapper;
import com.hzncc.scaffold.service.SiteapplyService;
import com.hzncc.scaffold.service.TagService;
import com.hzncc.scaffold.service.UserService;
import com.hzncc.scaffold.service.UsertagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger.web.TagsSorter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: DrunkBear
 * @Date: 2022-05-24 16:25
 * @Description: null
 */
@Api(tags = "1+X管理-站点管理")
@RestController
@RequestMapping("/manage/site")
public class siteManagementController {

    @Autowired
    SiteapplyService siteapplyService;

    @Autowired
    UserService userService;

    @Autowired
    SiteapplyMapper siteapplyMapper;

    @Autowired
    UsertagService usertagService;

    @Autowired
    TagService tagService;

    @GetMapping("/list")
    @ApiOperation("站点管理列表")
    public ResultT list(Integer pageNum, Integer pageSize, Integer type, Integer status, String keyword) {

        if (type == null) {
            return ResultT.fail("数据错误");
        }
        if (type != 0 && type != 1 && type != 2) {
            return ResultT.fail("数据错误");
        }

        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        QueryWrapper<SiteManagementBo> qw = new QueryWrapper<>();

        if (status != null) {
            qw.eq("siteapply.status", status);
        }
        if (StrUtil.isNotEmpty(keyword)) {
            qw.like("name", keyword);
        }
        qw.eq("type", type);
        List<SiteManagementBo> siteapplyList = siteapplyMapper.listBo(qw);
        ArrayList<SiteManagementListVo> SiteManagementListVos = new ArrayList<>();
        for (SiteManagementBo siteapply : siteapplyList) {
            SiteManagementListVo siteManagementListVo = BeanUtil.toBean(siteapply, SiteManagementListVo.class);

            PublisherVo publisherVo = new PublisherVo();
            User institute = new User();
            if (userService.getById(siteapply.getPublisherId()) != null) {
                User publisher = userService.getById(siteapply.getPublisherId());
                publisherVo.setId(publisher.getId());
                publisherVo.setName(publisher.getName());
            }
            if (userService.getById(siteapply.getInstituteId()) != null) {
                institute = userService.getById(siteapply.getInstituteId());
            }

            siteManagementListVo.setInstitute(institute);
            siteManagementListVo.setPublisher(publisherVo);

            SiteManagementListVos.add(siteManagementListVo);
        }
//        PageInfo<SiteManagementListVo> info = new PageInfo<>();
//        info.setList(SiteManagementListVos);
//        info.setTotal(new PageInfo<>(siteapplyList).getTotal());

        HashMap<String,Object> map = new HashMap<>();
        map.put("rows",SiteManagementListVos);
        map.put("count",new PageInfo<>(siteapplyList).getTotal());
        return ResultT.success(map);
    }

    @GetMapping("/detail")
    @ApiOperation("获取站点管理详情")
    public ResultT detail(Integer siteId) {

        if (siteapplyService.getById(siteId) == null) {
            return ResultT.fail("数据错误");
        }
        Siteapply siteapply = siteapplyService.getById(siteId);
        SiteManagementDetailVo siteManagementDetailVo = BeanUtil.toBean(siteapply, SiteManagementDetailVo.class);


        User institute = new User();
        if (userService.getById(siteapply.getInstituteId()) != null) {
            institute = userService.getById(siteapply.getInstituteId());
        }

        siteManagementDetailVo.setInstitute(institute);
        return ResultT.success(siteManagementDetailVo);
    }

    @PostMapping("/updateStatus")
    @ApiOperation("修改站点状态")
    public ResultT updateStatus(@RequestBody UpdateStatusVo updateStatusVo) {

        Integer applyId = updateStatusVo.getApplyId();
        Integer status = updateStatusVo.getStatus();


        if (siteapplyService.getById(applyId) == null) {
            return ResultT.fail("数据错误");
        }

        Siteapply siteapply = siteapplyService.getById(applyId);
        siteapply.setStatus(status);
        boolean update = siteapplyService.updateById(siteapply);
        if (update) {
            if (status == 1) {
                LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<>();
                if (siteapply.getType() == 0) {
                    lqw.eq(Tag::getName, "试点院校");
                }
                if (siteapply.getType() == 1) {
                    lqw.eq(Tag::getName, "考核站点");
                }
                if (siteapply.getType() == 2) {
                    lqw.eq(Tag::getName, "管理站点");
                }
                Tag tag = tagService.getOne(lqw);

                Usertag usertag = new Usertag();
                usertag.setUserId(applyId);
                usertag.setTagId(tag.getId());

                usertagService.save(usertag);
            }

            if (status == 2) {
                LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<>();
                if (siteapply.getType() == 0) {
                    lqw.eq(Tag::getName, "试点院校");
                }
                if (siteapply.getType() == 1) {
                    lqw.eq(Tag::getName, "考核站点");
                }
                if (siteapply.getType() == 2) {
                    lqw.eq(Tag::getName, "管理站点");
                }
                Tag tag = tagService.getOne(lqw);

                LambdaQueryWrapper<Usertag> lqww = new LambdaQueryWrapper<>();
                lqww.eq(Usertag::getTagId, tag.getId());
                usertagService.remove(lqww);
            }

            return ResultT.success("修改成功");
        } else {
            return ResultT.fail("修改失败");
        }
    }


    @PostMapping("/delete")
    @ApiOperation("删除站点")
    public ResultT deleteSite(@RequestBody ApplyIdVo applyIdVo) {

        Integer applyId = applyIdVo.getApplyId();

        if (siteapplyService.getById(applyId) == null) {
            return ResultT.fail("数据错误");
        }
        Siteapply siteapply = siteapplyService.getById(applyId);
        boolean remove = siteapplyService.removeById(applyId);

        if (remove) {
            LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<>();
            if (siteapply.getType() == 0) {
                lqw.eq(Tag::getName, "试点院校");
            }
            if (siteapply.getType() == 1) {
                lqw.eq(Tag::getName, "考核站点");
            }
            if (siteapply.getType() == 2) {
                lqw.eq(Tag::getName, "管理站点");
            }
            Tag tag = tagService.getOne(lqw);

            LambdaQueryWrapper<Usertag> lqww = new LambdaQueryWrapper<>();
            lqww.eq(Usertag::getTagId, tag.getId());
            usertagService.remove(lqww);

            return ResultT.success("删除成功");
        } else {
            return ResultT.fail("删除失败");
        }
    }
}
